package com.cloudcc.frame.comclass;

import com.cloudcc.deveditor.core.CCObject;
import com.cloudcc.deveditor.core.CCService;
import com.cloudcc.deveditor.core.UserInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.List;

public class FindMapInfoTest {


    private UserInfo userInfo;
    private CCService cs;

    public FindMapInfoTest(UserInfo userInfo) {
        this.userInfo = userInfo;
        cs = new CCService(userInfo);
    }

    public String findMap(String latitude) {
        String userId = userInfo.getUserId();
        String[] split = latitude.split(",");
//       经度
        String lat = split[0];
//       纬度
        String lon = split[1];

//        上下0.5
        Double new_lat = Double.valueOf(lat);
        Double new_Add_lat = new_lat + 0.5;
        Double new_deleted_lat = new_lat - 0.5;
        Double new_lon = Double.valueOf(lon);
        Double new_Add_lon = new_lon + 0.5;
        Double new_deleted_lon = new_lon - 0.5;
        String findManage_groupSql = "select id,fk_account,lxdz,dlwz,fk_ccuser,fk_psmc from manage_group where dlwzla > '" + new_deleted_lat + "' and dlwzla < '" + new_Add_lat + "' and dlwzco > '" + new_deleted_lon + "' and dlwzco < '" + new_Add_lon + "' and is_deleted='0' and fk_ccuser ='" + userId + "' ";
        List<CCObject> manageGroup = cs.cqlQuery("manage_group", findManage_groupSql);

        JSONObject returnDataInfo = new JSONObject();
        if (manageGroup.size() > 0) {
            JSONArray AccountInfoList = new JSONArray();
            JSONArray EleoppInfoList = new JSONArray();

            for (int i = 0; i < manageGroup.size(); i++) {
                String fk_ccuser = manageGroup.get(i).get("fk_ccuser") == null ? "" : manageGroup.get(i).get("fk_ccuser") + "";
                String fk_psmc = manageGroup.get(i).get("fk_psmc") == null ? "" : manageGroup.get(i).get("fk_psmc") + "";
                String manage_lxdz = manageGroup.get(i).get("lxdz") == null ? "" : manageGroup.get(i).get("lxdz") + "";
                String manage_dlwz = manageGroup.get(i).get("dlwz") == null ? "" : manageGroup.get(i).get("dlwz") + "";
                String id = manageGroup.get(i).get("id") == null ? "" : manageGroup.get(i).get("id") + "";
                String findEleNum = "select frequece from ele_num where fk_psmc ='"+fk_psmc+"' and fk_account ='"+fk_ccuser+"' and is_deleted='0' ";
                String pc = "";
                List<CCObject> ele_num = cs.cqlQuery("ele_num", findEleNum);
                if (ele_num.size()>0){
                    pc = ele_num.get(0).get("frequece")==null?"":ele_num.get(0).get("frequece")+"";
                }
                String findAccountInfo = "select name,follow_status,deal_status from Account where registeredAddress ='" + manage_lxdz + "' and is_deleted='0' and ownerid ='" + userId + "' ";
                List<CCObject> ccObjects = cs.cqlQuery("Account", findAccountInfo);
                if (ccObjects.size() > 0) {
                    for (int j = 0; j < ccObjects.size(); j++) {
                        JSONObject AccountInfo = new JSONObject();
                        AccountInfo.put("name", ccObjects.get(j).get("name"));
                        AccountInfo.put("khgjzt", ccObjects.get(j).get("follow_status"));
                        AccountInfo.put("khkhzt", ccObjects.get(j).get("deal_status"));
                        AccountInfo.put("jwd", manage_dlwz);
                        AccountInfo.put("GroupId", id);
                        AccountInfo.put("elepc", pc);
                        AccountInfoList.add(AccountInfo);
                    }
                }

                String findEleoppInfo = "select name,jieduan from eleopp where contact_address = '" + manage_lxdz + "' and is_deleted='0' and ownerid ='" + userId + "'";
                List<CCObject> eleopp = cs.cqlQuery("eleopp", findEleoppInfo);
                if (eleopp.size() > 0) {
                    for (int j = 0; j < eleopp.size(); j++) {
                        JSONObject eleoppInfoData = new JSONObject();
                        eleoppInfoData.put("name", eleopp.get(j).get("name"));
                        eleoppInfoData.put("jd", eleopp.get(j).get("jieduan"));
                        eleoppInfoData.put("jws", manage_dlwz);
                        eleoppInfoData.put("GroupId", id);
                        EleoppInfoList.add(eleoppInfoData);
                    }
                }


            }
            returnDataInfo.put("code", "200");

            returnDataInfo.put("Account", AccountInfoList);
            returnDataInfo.put("Eleopp", EleoppInfoList);
        }

        return String.valueOf(returnDataInfo);
    }

    //更新地址
    public String updateAddress(String data) {
        String userId = userInfo.getUserId();
        JSONObject jsonObject = JSONObject.fromObject(data);
        JSONObject JS = new JSONObject();
        String fk_psmc_id = "";
        String fk_account_id = "";
        String fk_ccuser_id = "";
        try {
            if (jsonObject.size() > 0) {
                //管户小组id
                String id = jsonObject.getString("id") == null ? "" : jsonObject.getString("id") + "";
                //地址
                String address = jsonObject.getString("address") == null ? "" : jsonObject.getString("address") + "";
                //商机状态
                String status1 = jsonObject.getString("status1") == null ? "" : jsonObject.getString("status1") + "";
                //根据管户小组id查询公共资源交易中心、关联客户、管户经理
                String manage_group_sql = "select fk_psmc,fk_account,fk_ccuser from manage_group where id='" + id + "' " +
                        "and is_deleted='0'";
                List<CCObject> manage_group_list = cs.cqlQuery("manage_group", manage_group_sql);
                if (manage_group_list.size() > 0) {
                    fk_psmc_id = manage_group_list.get(0).get("fk_psmc") == null ? "" : manage_group_list.get(0).get("fk_psmc") + "";
                    fk_account_id = manage_group_list.get(0).get("fk_account") == null ? "" : manage_group_list.get(0).get("fk_account") + "";
                    fk_ccuser_id = manage_group_list.get(0).get("fk_ccuser") == null ? "" : manage_group_list.get(0).get("fk_ccuser") + "";
                    String manage_group_update = "update manage_group set lxdz='" + address + "' " +
                            "where fk_account='" + fk_account_id + "' " +
                            "and is_deleted='0' " +
                            "and fk_ccuser='" + fk_ccuser_id + "' " +
                            "and fk_psmc='" + fk_psmc_id + "'";
                    cs.cqlQuery("manage_group", manage_group_update);

                    //更改电子保函商机地址
                    String updateEleoppAddress = "update eleopp set contact_address ='" + address + "' where khmc='" + fk_account_id + "' and is_deleted='0' " +
                            "and status1='" + status1 + "' " +
                            "and ownerid='" + userId + "' " +
                            "and fk_psmc='" + fk_psmc_id + "'";
                    cs.cqlQuery("eleopp", updateEleoppAddress);
                    JS.put("msg", "success");
                    JS.put("code", "200");
                }
            } else {
                JS.put("msg", "failure");
                JS.put("code", "404");
            }
        } catch (Exception e) {
            JS.put("msg", e.toString());
        }
        return String.valueOf(JS);
    }

    //添加活动（事件）
    //id,其它字段，
    public String addevent(String data) {
        JSONObject jsonObject = JSONObject.fromObject(data);
        //相关项id
        String relateid = jsonObject.getString("id");
        //类型
        String type = jsonObject.getString("type");
        //被分配人
        String belongtoid = jsonObject.getString("belongtoid");
        //主题(下拉)
        String sub = jsonObject.getString("sub");
        //主题
        String subject = jsonObject.getString("subject");
        //事件名称
        String position = jsonObject.getString("position");
        //内容
        String remark = jsonObject.getString("remark");
        //电子保函推荐事件
        String recordtype = "20211390340C7F7B9Sa4";


        String obj_suffix = "";
        if (!"".equals(relateid)) {
            obj_suffix = relateid.substring(0, 3);
        }
        String sql2 = "select SCHEMETABLE_NAME api from tp_sys_object where prefix='" + obj_suffix + "'";
        List<CCObject> list2 = cs.cqlQuery("works", sql2);
        String api = "";
        if (list2.size() > 0) {
            api = list2.get(0).get("api") == null ? "" : list2.get(0).get("api") + "";
        }
        JSONObject jsonObject1 = new JSONObject();
        try {
            if ("Account".equals(api)) {
                CCObject event = new CCObject("Event");
                event.put("type", type);
                event.put("belongtoid", belongtoid);
                event.put("sub", sub);
                event.put("subject", subject);
                event.put("position", position);
                event.put("remark", remark);
                event.put("recordtype", recordtype);
                event.put("ownerid", userInfo.getUserId());
                cs.insert(event);
                jsonObject1.put("msg", "success");
                jsonObject1.put("code", "200");
            } else if ("eleopp".equals(api)) {
                CCObject event = new CCObject("Event");
                event.put("type", type);
                event.put("belongtoid", belongtoid);
                event.put("sub", sub);
                event.put("subject", subject);
                event.put("position", position);
                event.put("remark", remark);
                event.put("recordtype", recordtype);
                event.put("ownerid", userInfo.getUserId());
                cs.insert(event);
                jsonObject1.put("msg", "success");
                jsonObject1.put("code", "200");
            } else {
                jsonObject1.put("msg", "failure");
                jsonObject1.put("code", "404");
            }
        } catch (Exception e) {
            jsonObject1.put("msg", e.toString());
        }
        return String.valueOf(jsonObject1);
    }


}

